const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function json2Form(json) {
  var str = [];
  for (var p in json) {
    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));
  }
  return str.join("&");
}
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

//是否为空字符串...
function isNull(data) {
  return data == null || data == '' || data == undefined;
}

//如果是undefined 或 null 显示空字符串
function ifNullToNullString(data) {
  return isNull(data) ? '' : data;
}

function getOssSmallPicUrl(originUrl) {
  return originUrl + "?x-oss-process=image/resize,m_fill,w_200,h_240,limit_0";
}
// 获取当前时间
function getFomcatNowTime() {
  var date = new Date();
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  const millisecond = date.getMilliseconds();
  return "" + year + month + day + hour + minute + second + millisecond;
}

function getFileNameFromUrl(url) {
  url = url.substring(url.lastIndexOf("/") + 1, url.length);
  return url;
}

function previewImg(currentUrl, imgArr) {
  wx.previewImage({
    current: currentUrl, //当前图片地址
    urls: imgArr, //所有要预览的图片的地址集合 数组形式
    success: function(res) {},
    fail: function(res) {},
    complete: function(res) {},
  })
}

//预览文件
function previewFile(url) {
  wx.downloadFile({
    url: url,
    success: function(res) {
      const filePath = res.tempFilePath
      wx.openDocument({
        filePath: filePath,

        success: function(res) {
          console.log('打开文档成功')
        }
      })
    }
  })
}

function isIdCardNo(card) {
  // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X 
  var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  return reg.test(card);
}

//把值丢到剪切板里去
function setClipboardData(dataString) {
  wx.setClipboardData({
    data: dataString,
    success: function(res) {
      wx.showToast({
        title: '复制成功',
      });
    }
  });
}

/**
 * 获取文件后缀
 * url:文件的路径，可以是网络路径，也可以是本地路径
 * 
 * return :  [.jpg] [.pdf]
 */
function getFileSuffix(url) {
  url = url.substring(url.lastIndexOf("."), url.length);
  return url;
}
module.exports = {
  json2Form: json2Form,
  formatTime: formatTime,
  getFileSuffix: getFileSuffix,
  getFomcatNowTime: getFomcatNowTime,
  isNull: isNull,
  getOssSmallPicUrl: getOssSmallPicUrl,
  previewImg: previewImg,
  getFileNameFromUrl: getFileNameFromUrl,
  previewFile: previewFile,
  isIdCardNo: isIdCardNo,
  setClipboardData: setClipboardData,
  ifNullToNullString: ifNullToNullString
}